package com.sixmac.controller;

import com.sixmac.annotation.AuthUser;
import com.sixmac.common.base.BaseController;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by admin on 2018/9/4.
 */
@Controller
@RequestMapping("/region")
@ResponseBody
public class RegionController extends BaseController {
    /**
     * @api {post} /region/province 0.1获取省
     * @apiName region.province
     * @apiGroup region
     * @apiParam {String} token token
     * @apiSuccess {String} user.provinceCode provinceCode
     * @apiSuccess {String} user.province 名字
     */
    @AuthUser
    @RequestMapping("/province")
    public AjaxResult getProvince() {
        return json(getData("province", Db.selectList("select provinceCode,province from t_region_province")));
    }

    /**
     * @api {post} /region/getCities 三级联动
     * @apiName region.province
     * @apiGroup region
     * @apiParam {String} token token
     */
    @RequestMapping("/getCities")
    public AjaxResult getCities() {
        List<Map> cities = Db.selectList("SELECT p.province,p.provinceCode,c.city,c.cityCode,a.area,a.areaCode from \n" +
                "t_region_province p LEFT JOIN t_region_city c ON p.provinceCode = c.provinceCode\n" +
                "LEFT JOIN t_region_area a ON c.cityCode = a.cityCode");

        List<HashMap<String,Object>> citys = new ArrayList<>();
        LinkedHashMap<String,HashMap<String,Object>> object= new LinkedHashMap<>();
        for (Map map:cities){
            String province = map.get("province").toString();
            String provinceCode = map.get("provinceCode").toString();
            String city = map.get("city")==null?province:map.get("city").toString();
            String cityCode =map.get("cityCode")==null?provinceCode:map.get("cityCode").toString();
            String areaCode =map.get("areaCode")==null?cityCode:map.get("areaCode").toString();
            String area =map.get("area")==null?city:map.get("area").toString();
            HashMap provinceMap;
            if (!object.containsKey(provinceCode)){
                object.put(provinceCode,new HashMap<String,Object>());
            }
            provinceMap = object.get(provinceCode);
            provinceMap.put("province",province);
            provinceMap.put("provinceCode",provinceCode);
            ArrayList<HashMap> cityMap;
            if (!provinceMap.containsKey("cities")){
                provinceMap.put("cities",new ArrayList<HashMap>());
            }
            cityMap = (ArrayList<HashMap>) provinceMap.get("cities");
            ArrayList<HashMap<String,Object>> aras = null;
            for (HashMap c:cityMap) {
                if (c.get("cityCode").toString().equals(cityCode)){
                    aras = (ArrayList<HashMap<String, Object>>) c.get("aras");
                    break;
                }
            }
            if (aras == null){
                aras = new ArrayList<>();
                HashMap<String,Object> c = new HashMap<>();
                c.put("cityCode",cityCode);
                c.put("city",city);
                c.put("aras",aras);
                cityMap.add(c);
            }
            HashMap<String,Object> as = new HashMap<>();
            as.put("area",area);
            as.put("areaCode",areaCode);
            aras.add(as);
        }

        Iterator iter = object.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            citys.add((HashMap)(entry.getValue()));
        }

        return json(citys);
    }

    /**
     * @api {post} /region/city 0.2获取市
     * @apiName region.city
     * @apiGroup region
     * @apiParam {String} token token
     * @apiParam {String} provinceCode provinceCode
     * @apiSuccess {String} user.cityCode cityCode
     * @apiSuccess {String} user.city 名字
     */
    @AuthUser
    @RequestMapping("/city")
    public AjaxResult city(String provinceCode) {
        return json(getData("province", Db.selectList("select cityCode,city from t_region_city where provinceCode=#{provinceCode}", getData("provinceCode", provinceCode))));
    }

    /**
     * @api {post} /region/area 0.3获取区
     * @apiName region.area
     * @apiGroup region
     * @apiParam {String} token token
     * @apiParam {String} cityCode cityCode
     * @apiSuccess {String} user.areaCode areaCode
     * @apiSuccess {String} user.city 名字
     */
    @AuthUser
    @RequestMapping("/area")
    public AjaxResult area(String cityCode) {
        return json(getData("cityCode", Db.selectList("select areaCode,area from t_region_area where cityCode=#{cityCode}", getData("cityCode", cityCode))));
    }


}
